package cn.lili.eventbus.handlers;

import cn.lili.consumer.event.AfterSaleStatusChangeEvent;
import cn.lili.eventbus.event.AfterSaleEvent;
import cn.lili.eventbus.tags.AfterSaleTagsEnum;
import cn.lili.modules.order.aftersale.entity.dos.AfterSale;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Sylow
 * @Description
 * @Date: Created in 14:56 2025/6/19
 */
@Slf4j
@Service
public class AfterSaleEventHandler {

    /**
     * 售后订单状态
     */
    @Autowired
    private List<AfterSaleStatusChangeEvent> afterSaleStatusChangeEvents;

    @EventListener
    public void handleAfterSaleEvent(AfterSaleEvent event) {
        if (event.getAfterSaleTagsEnum() == AfterSaleTagsEnum.AFTER_SALE_STATUS_CHANGE) {
            for (AfterSaleStatusChangeEvent afterSaleStatusChangeEvent : afterSaleStatusChangeEvents) {
                try {
                    AfterSale afterSale = (AfterSale) event.getData();
                    afterSaleStatusChangeEvent.afterSaleStatusChange(afterSale);
                } catch (Exception e) {
                    log.error("售后{},在{}业务中，状态修改事件执行异常",
                            ((AfterSale) event.getData()).getSn(),
                            afterSaleStatusChangeEvent.getClass().getName(),
                            e);
                }
            }
        }
    }
}
